package com.example.guet_assistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.guet_assistant.context.BaseContext;
import com.example.guet_assistant.domain.dto.PageDTO;
import com.example.guet_assistant.domain.po.CommonTool;
import com.example.guet_assistant.domain.po.User;
import com.example.guet_assistant.domain.vo.PageVO;
import com.example.guet_assistant.mapper.CommonToolMapper;
import com.example.guet_assistant.pojo.entity.Tool;
import com.example.guet_assistant.service.ToolService;
import com.example.guet_assistant.mapper.ToolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【tool】的数据库操作Service实现
* @createDate 2024-11-03 15:54:17
*/
@Service
public class ToolServiceImpl extends ServiceImpl<ToolMapper, Tool> implements ToolService{
    @Autowired
    private ToolMapper toolMapper;


    @Autowired
    private CommonToolMapper commonToolMapper;

    @Override
    public List<Tool> getUserTools(Long userId) {
        return toolMapper.getUserTools(userId);
    }


    @Override
    public PageVO<Tool> getCommonTools(PageDTO pageDTO) {
           Page<Tool> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
                      page=page(page);
           return   PageVO.<Tool>builder()
                   .tools(page.getRecords())
                   .total(page.getTotal())
                   .build();
    }





    @Override
    @Transactional
    public void addUserTools(List<Integer> ids) {
        Integer currentId = BaseContext.getCurrentId();
        for (Integer id : ids){
               commonToolMapper.insert(CommonTool.builder().toolId(id).userId(currentId).build());
        }

    }


    @Override
    @Transactional
    public void removeUserTools(List<Integer> ids) {
        Integer currentId = BaseContext.getCurrentId();
        for (Integer id : ids){
            LambdaQueryWrapper<CommonTool> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(CommonTool::getToolId,id)
                    .eq(CommonTool::getUserId,currentId);
            commonToolMapper.delete(lambdaQueryWrapper);
        }
    }


}




